package client

import (
	"context"
	"dh.com/store/srv/goods/proto/goods"

	"github.com/micro/go-micro"
	"github.com/micro/go-micro/server"
)

type goodsKey struct{}

// FromContext retrieves the client from the Context
func GoodsFromContext(ctx context.Context) (goods.GoodsService, bool) {
	c, ok := ctx.Value(goodsKey{}).(goods.GoodsService)
	return c, ok
}

// Client returns a wrapper for the GoodsClient
func GoodsWrapper(service micro.Service) server.HandlerWrapper {
	client := goods.NewGoodsService("com.dh.store.srv.goods", service.Client())

	return func(fn server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
			ctx = context.WithValue(ctx, goodsKey{}, client)
			return fn(ctx, req, rsp)
		}
	}
}
